// pages/couponCenter/couponCenter.js

var http = require("../../utils/http.js");
var config = require("../../utils/config.js");
var util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    prodCouponList: [], // 商品券列表
    selectedCouponId:0, // 要领取的优惠券id
    current: 1, // 当前页数
    pages: 1, // 总页数
    loadAll: false, // 已加载全部
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //获取通用列表


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getProdCouponList(1);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 获取指定商品券
   */
  getProdCouponList(cur) {
    var params = {
      url: "/p/myCoupon/getCouponPage",
      method: "GET",
      data: {
        current: cur,
        size: 20,
      },
      callBack: (res) => {
        this.initCouponCanGoUseFlag(res.records)
        let list = []
        if (res.current == 1) {
          list = res.records
        } else {
          list = this.data.prodCouponList
          list = list.concat(res.records)
        }
        this.setData({
          prodCouponList: list,
          pages: res.pages,
          current: res.current
        });
      }
    };
    http.request(params);
  },
  /**
   * 初始化优惠券去可以使用的标记
   */
  initCouponCanGoUseFlag(couponList){
    couponList.forEach(coupon => {
      coupon.canGoUse = (coupon.curUserReceiveCount >= coupon.limitNum)
    });
  },
  /**
   * 设置优惠券去可以使用的标记
   */
  setCouponCanGoUseFlag(index, couponType) {
    if (couponType == 1) {
      var tempCouponList = this.data.couponList
      tempCouponList[index].canGoUse = true
      tempCouponList[index].stocks -= 1
      this.setData({
        couponList: tempCouponList
      })
    } else if (couponType == 2) {
      var tempCouponList = this.data.prodCouponList
      tempCouponList[index].canGoUse = true
      tempCouponList[index].stocks -= 1
      this.setData({
        prodCouponList: tempCouponList
      })
    }
  },
  /**
   * 立即领取
   */
  receiveCoupon(e) {
    this.setData({
      selectedCouponId: e.currentTarget.dataset.couponid
    })
    util.checkAuthInfo(()=>{
      var ths = this
      if (ths.data.selectedCouponId) {
        wx.showLoading();
        http.request({
          url: "/p/myCoupon/receive",
          method: "POST",
          data: ths.data.selectedCouponId,
          callBack: (data) => {
            wx.hideLoading();
            wx.showToast({
              title: '领券成功',
              icon: 'success',
              duration: 2000
            })
            this.setCouponCanGoUseFlag(e.currentTarget.dataset.couponindex,e.currentTarget.dataset.coupontype)
          }
        })
      }
    })
  },
  /**
   * 立即使用
   */
  useCoupon(e) {
    let url = '/pages/prod-classify/prod-classify?sts=4';
    let couponId = e.currentTarget.dataset.couponid;
    var title = "优惠券活动商品";
    var prodList = e.currentTarget.dataset.prodlist
    if (prodList && prodList.length == 1) {
      wx.navigateTo({
        url: '/pages/prod/prod?prodid=' + prodList[0].prodId
      })
    }else {
      if (couponId) {
        url += "&tagid=" + couponId + "&title=" + title;
      }
      wx.navigateTo({
        url: url
      })
    }
  },
    /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.current < this.data.pages) {
      this.getProdCouponList(this.data.current + 1);
    }else {
      this.setData({
        loadAll: true
      })
    }
  },

})